/*******************************************************************************
 * Copyright (c) 2009-2011 ETCORE Labs. All rights reserved. 
 * 
 * This software is the confidential and proprietary information of ETCORE Labs.
 * You shall not disclose such confidential information and shall use it
 * only in accordance with the terms of the license agreement you entered into
 * with ETCORE Labs. 
 * 
 * Support can be obtained from support forums at:  http://www.etcore.com/
 * /
 *******************************************************************************/
package etcore.webapp.grouptalk.user.action;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import etcore.webapp.grouptalk.common.action.AbstractAction;
import etcore.webapp.grouptalk.common.config.PageConstants;
import etcore.webapp.grouptalk.common.object.Group;
import etcore.webapp.grouptalk.common.service.GroupService;
import etcore.webapp.grouptalk.user.object.Member;
import etcore.webapp.grouptalk.user.object.User;
import etcore.webapp.grouptalk.user.service.MemberService;
import etcore.webapp.grouptalk.user.service.UserService;

/**
 * This action validates the request and executes the command of showing the
 * welcome/home page,then set the corresponding attributes for output
 * 
 * @author ETCORE GroupTalk Developers
 * 
 */
public class GroupFollowersAction extends AbstractAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = -1020089027738044162L;

	// validate result

	public static final String GROUP_FOLLOWERS = "groupFollowers";
	public static final String CMD_REMOVE = "remove";
	public static final String CMD_ACCEPT = "accept";
	public static final String TWO = "2";
	// validate result
	public static final int ACCESS_ALLOWED = 0;
	public static final int ERROR = 1;

	// utility constants

	@Override
	public int validate(HttpServletRequest request, HttpServletResponse response) {
		// TODO Auto-generated method stub
		String gid = request.getParameter(PageConstants.GID);
		Group group = null;
		GroupService groupService = new GroupService();
		request.setAttribute("manage", false);
		
		if (gid != null) {
			group = groupService.findById(new Integer(gid));
			if (group != null && group.getOpen() == 2) {

				HttpSession session = request.getSession(true);
				User user = (User) session
						.getAttribute(PageConstants.SESSIONUSER);
				if (user != null && user.getIsLock() != 1) {
					MemberService memberService = new MemberService();
					Member member = memberService.findByUsernameAndGid(
							user.getUserName(), new Integer(gid));

					if (member != null
							&& (member.getMytype().equals(Member.ADMINISTRATOR) || member
									.getMytype().equals(Member.OWNER)))
						request.setAttribute("manage", true);

				}
				boolean isAdmin = (Boolean) request.getAttribute(PageConstants.ISADMIN);
				if (isAdmin)
					request.setAttribute("manage", true);
				return ACCESS_ALLOWED;
			}
		}

		return ERROR;

	}

	@Override
	public String getForward(HttpServletRequest request, int validationResult) {
		if (ERROR == validationResult)
			return PageConstants.ACTION_PATH_ERROR;
		return PageConstants.PAGE_PATH_GROUPFOLLOWERS;
	}

	public void getFollowers(HttpServletRequest request) {
		MemberService memberService = new MemberService();
		String gid = request.getParameter(PageConstants.GID);

		int intpageid = 1;
		Integer gidInteger = new Integer(gid);
		int unit = 10, unit2 = 5;
		String unitStr = request.getParameter(PageConstants.UNIT);
		if (unitStr != null)
			unit = Integer.parseInt(unitStr);
		long all = 0;
		int begin, pagenums;
		String pageid = request.getParameter(PageConstants.PAGEID);
		if (pageid != null)
			intpageid = Integer.parseInt(pageid);
		all = memberService.getAllMemberNum(gidInteger);
		pagenums = (int) ((all + unit - 1) / unit);
		begin = unit * (intpageid - 1);
		List<User> groupFollowers = new ArrayList<User>();
		UserService userService = new UserService();
		List<Member> members = memberService.getAllMember(gidInteger, begin,
				unit);
		if (members != null) {
			for (Member member : members) {
				User user = userService.findByUserName(member.getUserName());
				if (user != null)
					groupFollowers.add(user);
			
			}
		}

		int startpid = intpageid - 2;
		if (startpid <= 0)
			startpid = 1;
		int endpid = intpageid + 1;
		if (intpageid > 3)
			endpid = intpageid + 2;
		if (endpid >= pagenums)
			endpid = pagenums;

		request.setAttribute(PageConstants.START_PID, startpid);
		request.setAttribute(PageConstants.END_PID, endpid);

		request.setAttribute(PageConstants.PAGENUMS, pagenums);
		request.setAttribute(PageConstants.ALL, all);
		request.setAttribute(PageConstants.UNIT, unit);
		request.setAttribute(PageConstants.INTPAGEID, intpageid);
		request.setAttribute("groupFollowers", groupFollowers);

		long all2 = memberService.getLockedMemberNum(gidInteger);
		int begin2 = 0, pagenums2 = 0, intpageid2 = 1;
		String pageid2 = request.getParameter(PageConstants.PAGEID + TWO);
		if (pageid2 != null)
			intpageid2 = Integer.parseInt(pageid2);

		pagenums2 = (int) ((all2 + unit2 - 1) / unit2);
		begin2 = unit * (intpageid2 - 1);
		List<User> restrictedFollowers = new ArrayList<User>();
		List<Member> restricted = memberService.getLockedMember(gidInteger,
				begin2, unit2);
		if (restricted != null) {
			for (Member member : restricted) {
				User user = userService.findByUserName(member.getUserName());
				if (user != null)
					restrictedFollowers.add(user);
			}
		}
		int startpid2 = intpageid2 - 2;
		if (startpid2 <= 0)
			startpid2 = 1;
		int endpid2 = intpageid2 + 1;
		if (intpageid2 > 3)
			endpid2 = intpageid2 + 3;
		if (endpid2 >= pagenums2)
			endpid2 = pagenums2;
		request.setAttribute(PageConstants.START_PID + TWO, startpid2);
		request.setAttribute(PageConstants.END_PID + TWO, endpid2);

		request.setAttribute("all2", all2);
		request.setAttribute("restrictedFollowers", restrictedFollowers);
		request.setAttribute(PageConstants.INTPAGEID + TWO, intpageid2);
		request.setAttribute(PageConstants.PAGENUMS + TWO, pagenums2);
	}

	public void manage(HttpServletRequest request) {
		 String cmd=request.getParameter(PageConstants.CMD);
		if(CMD_REMOVE.equals(cmd)||CMD_ACCEPT.equals(cmd)){
	
		 String userName=request.getParameter(PageConstants.USERNAME);
		 String gid = request.getParameter(PageConstants.GID);
		 MemberService memberService = new MemberService();
			Member member = memberService.findByUsernameAndGid(
					userName, new Integer(gid));
			if (CMD_REMOVE.equals(cmd)) {
				if (member != null && !member.getMytype().equals(Member.OWNER))
					memberService.deleteSingle(member);
			} else if (CMD_ACCEPT.equals(cmd)) {
				if (member != null) {
					member.setLocked(0);
					memberService.merge(member);
				}
			}
		}
			
	}

	@Override
	public String getRedirect(HttpServletRequest request, int validationResult) {
		// TODO Auto-generated method stub

		return null;
	}

	@Override
	public void execute(int validationResult, HttpServletRequest request,
			HttpServletResponse response) {
		boolean manage = (Boolean) request.getAttribute("manage");
		if(manage)
			manage(request);
		getFollowers(request);
	}

	@Override
	public boolean isForward(int validationResult) {
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	public boolean enablePostLoad() {
		// TODO Auto-generated method stub
		return false;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see etcore.webapp.grouptalk.controller.action.AbstractAction#
	 * buildSelfContextAndAttributes(int, javax.servlet.http.HttpServletRequest)
	 */
	@Override
	public List<String> getNamespacesOfMessages(HttpServletRequest request) {
		// TODO Auto-generated method stub
		return null;
	}

}
